package com.palace.saw.module.app.controller.app.articlecategory;

import com.palace.saw.module.app.controller.admin.articlecategory.vo.ArticleCategoryPageReqVO;
import com.palace.saw.module.app.controller.admin.articlecategory.vo.ArticleCategoryRespVO;
import com.palace.saw.module.app.controller.admin.articlecategory.vo.ArticleCategorySaveReqVO;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.palace.saw.framework.common.pojo.PageParam;
import com.palace.saw.framework.common.pojo.PageResult;
import com.palace.saw.framework.common.pojo.CommonResult;
import com.palace.saw.framework.common.util.object.BeanUtils;
import static com.palace.saw.framework.common.pojo.CommonResult.success;

import com.palace.saw.framework.excel.core.util.ExcelUtils;

import com.palace.saw.framework.apilog.core.annotation.ApiAccessLog;
import static com.palace.saw.framework.apilog.core.enums.OperateTypeEnum.*;

import com.palace.saw.module.app.dal.dataobject.articlecategory.ArticleCategoryDO;
import com.palace.saw.module.app.service.articlecategory.ArticleCategoryService;

@Tag(name = "用户 APP - 文章分类")
@RestController
@RequestMapping("/app/article-category")
@Validated
public class AppArticleCategoryController {

    @Resource
    private ArticleCategoryService articleCategoryService;

    @PostMapping("/create")
    @Operation(summary = "创建文章分类")
    public CommonResult<Long> createArticleCategory(@Valid @RequestBody ArticleCategorySaveReqVO createReqVO) {
        return success(articleCategoryService.createArticleCategory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新文章分类")
    public CommonResult<Boolean> updateArticleCategory(@Valid @RequestBody ArticleCategorySaveReqVO updateReqVO) {
        articleCategoryService.updateArticleCategory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除文章分类")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteArticleCategory(@RequestParam("id") Long id) {
        articleCategoryService.deleteArticleCategory(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得文章分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<ArticleCategoryRespVO> getArticleCategory(@RequestParam("id") Long id) {
        ArticleCategoryDO articleCategory = articleCategoryService.getArticleCategory(id);
        return success(BeanUtils.toBean(articleCategory, ArticleCategoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得文章分类分页")
    public CommonResult<PageResult<ArticleCategoryRespVO>> getArticleCategoryPage(@Valid ArticleCategoryPageReqVO pageReqVO) {
        PageResult<ArticleCategoryDO> pageResult = articleCategoryService.getArticleCategoryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ArticleCategoryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出文章分类 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportArticleCategoryExcel(@Valid ArticleCategoryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ArticleCategoryDO> list = articleCategoryService.getArticleCategoryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "文章分类.xls", "数据", ArticleCategoryRespVO.class,
                        BeanUtils.toBean(list, ArticleCategoryRespVO.class));
    }

}